package com.yycx.module.bus.provider.handler.finance;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.provider.service.BusSfinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;

/**
 * 厂家对账上期列表
 *
 * @author zyf
 */
@Component("sfinacePageListHandler")
public class SfinacePageListHandler implements PageInterceptor<BusOrderDetails> {


    @Autowired
    private BusSfinanceService busSfinanceService;

    @Override
    public void prepare(CriteriaQuery<BusOrderDetails> cq, PageParams pageParams, EntityMap params) {
        Long sfinanceId = params.getLong("sfinanceId");
        BusSfinance benQiHjFinance = busSfinanceService.getById(sfinanceId);
        //本期月份
        String financeMonth = benQiHjFinance.getFinanceMonth();
        //上期日期
        LocalDate lastMonthDate = LocalDate.parse(financeMonth + "-01");
        Long supplierId = benQiHjFinance.getSupplierId();
        cq.addSelect(BusDept.class, "deptName");
        cq.addSelect(BusSaleman.class, "saleManName");
        cq.lt("bod.saleDate", lastMonthDate);
        cq.eq(true, "bod.supplierId", supplierId);
        cq.createJoin(BusDept.class);
        cq.createJoin(BusSaleman.class);
        cq.orderByDesc("saleDate");
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<BusOrderDetails> cq, List<EntityMap> result, EntityMap extra) {
        Long financeId = cq.getLong("sfinanceId");
        BusSfinance benQiHjFinance = busSfinanceService.getById(financeId);
        initBusSfinanceDetails(result, benQiHjFinance);
        return result;
    }


    /**
     * 计算本期明细
     *
     * @param orderDetailsList
     * @param benQiHjFinance
     * @return
     */
    private void initBusSfinanceDetails(List<EntityMap> orderDetailsList, BusSfinance benQiHjFinance) {

        //当期第一天
        String lastFirstDay = benQiHjFinance.getFinanceMonth() + "-01";
        if (FlymeUtils.isNotEmpty(orderDetailsList)) {
            for (EntityMap orderDetails : orderDetailsList) {

                String supplierRetBondDate = orderDetails.get("supplierRetBondDate");
                if (FlymeUtils.isNotEmpty(supplierRetBondDate)) {
                    //如果是本期退垫付款则不排除
                    if (DateUtils.ge(supplierRetBondDate, lastFirstDay)) {
                        orderDetails.put("supplierRetBondDate", "");
                        orderDetails.put("returnSupplierBond", 0);
                    }
                }
                String renkuanDate = orderDetails.get("renkuanDate");
                if (FlymeUtils.isNotEmpty(renkuanDate)) {
                    if (DateUtils.ge(renkuanDate+ "-01", lastFirstDay)) {
                        orderDetails.put("returnDate", "");
                        orderDetails.put("returnAmount", 0);
                    }
                }

                String supplierRetDate2 = orderDetails.get("supplierRetDate2");
                if (FlymeUtils.isNotEmpty(supplierRetDate2)) {
                    //返款信息
                    if (DateUtils.ge(supplierRetDate2+"-01", lastFirstDay)) {
                        orderDetails.put("supplierRetDate", "");
                        orderDetails.put("supplierRealAmount", 0);
                    }
                }
                String supplierBondPayDate = orderDetails.get("supplierBondPayDate");
                if (FlymeUtils.isNotEmpty(supplierBondPayDate)) {
                    //垫付信息
                    if (DateUtils.ge(supplierBondPayDate, lastFirstDay)) {
                        orderDetails.put("supplierBondPayDate", "");
                        orderDetails.put("supplierPayBond", 0);
                    }
                }
            }
        }
    }
}
